package com.zengde.ai_sandbox.service.strategy.impl;

import com.zengde.ai_sandbox.context.OrderContext;
import com.zengde.ai_sandbox.context.OrderContextHolder;
import com.zengde.ai_sandbox.entity.Order;
import com.zengde.ai_sandbox.factory.PaymentStrategyFactory;
import com.zengde.ai_sandbox.service.strategy.PaymentStrategy;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    private final PaymentStrategyFactory paymentStrategyFactory;

    public OrderService(PaymentStrategyFactory paymentStrategyFactory) {
        this.paymentStrategyFactory = paymentStrategyFactory;
    }

    public void processOrder(Order order) {
        // ... 其他订单逻辑 ...

        OrderContext orderContext =buildContext(order);

        //在最上游，将要的信息放进contextHolder中
//        OrderContext orderContext =OrderContextHolder.getOrderContextTL();

        //初始化context
        OrderContextHolder.setOrderContextTL(orderContext);

        // 核心：根据订单中的支付类型，获取对应的策略
        PaymentStrategy strategy = paymentStrategyFactory.getStrategy(order.getPaymentType());

        // 执行策略
        strategy.process();

        // ... 其他订单逻辑 ...
    }


    private OrderContext buildContext(Order order) {
        // 构建上下文对象
        OrderContext orderContext = new OrderContext();
        orderContext.setOrder(order);
        orderContext.setPaymentId("1234567890");
        orderContext.setActualPaymentAmount(order.getPaymentAmount());

        return orderContext;
        // ... 其他上下文属性 ...
    }

}